package com.csu.jpetstore_reconfiguration_back.Controller;

import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.User;
import com.csu.jpetstore_reconfiguration_back.service.OrderService;
import com.csu.jpetstore_reconfiguration_back.domain.Order;
import com.csu.jpetstore_reconfiguration_back.vo.OrderVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

    // 获取所有订单
    @GetMapping("/order")
    public CommonResponse<List<OrderVO>> getAllOrders(HttpServletRequest request)
    {
        return orderService.getAllOrders(request);
    }

    //获取单个订单
    @GetMapping("/order/{id}")
    public CommonResponse<OrderVO> getOrder(@PathVariable ("id")String id)
    {
        return orderService.getOrder(id);
    }

    //添加订单
    @PostMapping("/order")
    public CommonResponse<OrderVO> addOrder(@Validated @RequestBody OrderVO orderVo,HttpServletRequest request)
    {
//        System.out.println(orderVo);
        System.out.println(orderVo.getOrderLines());
        return orderService.addOrder(orderVo,request);
    }

    //更新订单
    @PostMapping("/order/{id}")
    public CommonResponse<OrderVO> updateOrder(@PathVariable String id,@Validated @RequestBody OrderVO orderVO)
    {
        return orderService.updateOrder(id,orderVO);
    }

    //删除订单
    @PostMapping("/order/{id}/delete")
    public CommonResponse<OrderVO> deleteOrder(@PathVariable String id)
    {
        return orderService.deleteOrder(id);
    }

}
